package com.ryskoo.framework.core.controller;

import com.ryskoo.framework.core.bean.*;
import com.ryskoo.framework.core.exception.InvalidParamException;
import com.ryskoo.framework.core.service.IBasicService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * 服务API基类，提供基础的API入口
 *
 * @param <PO> the type parameter
 * @author Shawn
 */
public class BasicAPI<PO extends BasicPO> {

    private static final Logger logger = LogManager.getLogger("ERROR");

    private IBasicService basicService;

    protected void setService(IBasicService basicService) {
        this.basicService = basicService;
    }

    /**
     * 根据id查询管理员
     *
     * @param id id 唯一标识
     * @return 统一返回消息 {@link ReturnMessageDTO}&lt;{@link PO}&gt;
     */
    @GetMapping("/get/{id}")
    public ReturnMessageDTO<PO> getById(@PathVariable String id) {
        ReturnMessageDTO<PO> returnMessage = new ReturnMessageDTO<>();
        returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
        returnMessage.setReturnNote("查询成功");
        returnMessage.setReturnMessage((PO) basicService.getByUuid(id));
        return returnMessage;
    }


    /**
     * 查询列表
     *
     * @param qm 统一查询消息 {@link QueryMessageDTO}
     * @return 统一返回消息 {@link ReturnMessageDTO}&lt;{@link List}&lt;{@link PO}&gt;&gt;
     */
    @PostMapping("/list")
    public ReturnMessageDTO<List<PO>> getList(@RequestBody QueryMessageDTO qm) {
        ReturnMessageDTO<List<PO>> returnMessage = new ReturnMessageDTO<>();
        List<PO> managerList = basicService.queryByConditionAndPage(qm);
        int count = basicService.getCount(qm);
        returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
        returnMessage.setReturnNote("查询成功");
        returnMessage.setReturnMessage(managerList);
        returnMessage.setPager(new Pager(qm.getPageShow(), qm.getStartNum(), count));
        return returnMessage;
    }


    /**
     * 新增
     *
     * @param po 实体对象 {@link PO}
     * @return 统一返回消息 {@link ReturnMessageDTO}&lt;{@link PO}&gt;
     */
    @PostMapping("/create")
    public ReturnMessageDTO<PO> create(@RequestBody PO po) {
        ReturnMessageDTO<PO> returnMessage = new ReturnMessageDTO<>();
        try {
            checkBeforeCreate(po);
            basicService.create(po);
            returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessage.setReturnNote("新增成功");
            returnMessage.setReturnMessage(po);
        } catch (InvalidParamException e) {
            returnMessage.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
            returnMessage.setReturnNote(e.getMessage());
        } catch (Exception e) {
            returnMessage.setReturnCode(ReturnCodeEnum.ERROR.getReturnCode());
            returnMessage.setReturnNote("新增失败");
            logger.error("新增失败", e);
        }
        return returnMessage;
    }

    /**
     * 新增前校验
     *
     * @param po
     */
    protected void checkBeforeCreate(PO po) throws InvalidParamException {

    }

    /**
     * 修改成功
     *
     * @param po 实体对象 {@link PO}
     * @return 统一返回消息 {@link ReturnMessageDTO}&lt;{@link PO}&gt;
     */
    @PostMapping("/update")
    public ReturnMessageDTO<PO> update(@RequestBody PO po) {
        ReturnMessageDTO<PO> returnMessage = new ReturnMessageDTO<>();
        try {
            checkBeforeUpdate(po);
            basicService.update(po);
            returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessage.setReturnNote("修改成功");
            returnMessage.setReturnMessage(po);
        } catch (InvalidParamException e) {
            returnMessage.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
            returnMessage.setReturnNote(e.getMessage());
        } catch (Exception e) {
            returnMessage.setReturnCode(ReturnCodeEnum.ERROR.getReturnCode());
            returnMessage.setReturnNote("修改失败");
            logger.error("修改失败", e);
        }
        return returnMessage;
    }

    /**
     * 修改前校验
     *
     * @param po
     * @throws InvalidParamException
     */
    protected void checkBeforeUpdate(PO po) throws InvalidParamException {

    }

    /**
     * 删除
     *
     * @param idList id列表 {@link List}&lt;{@link String}&gt;
     * @return 统一返回消息 {@link ReturnMessageDTO}&lt;{@link PO}&gt;
     */
    @PostMapping("/delete")
    public ReturnMessageDTO<PO> delete(@RequestBody List<String> idList) {
        ReturnMessageDTO<PO> returnMessage = new ReturnMessageDTO<>();
        try {
            checkBeforeDelete(idList);
            basicService.deletes(idList);
            returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessage.setReturnNote("删除成功");
        } catch (InvalidParamException e) {
            returnMessage.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
            returnMessage.setReturnNote(e.getMessage());
        } catch (Exception e) {
            returnMessage.setReturnCode(ReturnCodeEnum.ERROR.getReturnCode());
            returnMessage.setReturnNote(e.getMessage());
            logger.error("删除失败", e);
        }
        return returnMessage;
    }

    /**
     * 删除前校验
     *
     * @param idList
     */
    protected void checkBeforeDelete(List<String> idList) throws InvalidParamException {

    }


    /**
     * 不带条件查询所有对象
     *
     * @return 统一返回消息 {@link ReturnMessageDTO}&lt;{@link List}&lt;{@link PO}&gt;&gt;
     */
    @PostMapping("/getAll")
    public ReturnMessageDTO<List<PO>> getAll() {
        ReturnMessageDTO<List<PO>> returnMessage = new ReturnMessageDTO<>();
        List<PO> managerList = basicService.getAll();
        returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
        returnMessage.setReturnNote("查询成功");
        returnMessage.setReturnMessage(managerList);
        return returnMessage;
    }


    /**
     * 根据条件查询列表（不带分页）
     *
     * @param qm 统一查询消息 {@link QueryMessageDTO}
     * @return 统一返回消息 {@link ReturnMessageDTO}&lt;{@link List}&lt;{@link PO}&gt;&gt;
     */
    @PostMapping("/listAll")
    public ReturnMessageDTO<List<PO>> listAll(@RequestBody QueryMessageDTO qm) {
        ReturnMessageDTO<List<PO>> returnMessage = new ReturnMessageDTO<>();
        List<PO> managerList = basicService.queryByCondition(qm);
        returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
        returnMessage.setReturnNote("查询成功");
        returnMessage.setReturnMessage(managerList);
        return returnMessage;
    }
}
